package org.leumasjaffe.initiativeTracker.event;


import java.awt.event.ActionEvent;

import javax.swing.Action;

import org.leumasjaffe.initiativeTracker.Constants;
import org.leumasjaffe.initiativeTracker.operator.CharacterLinker;
import org.leumasjaffe.initiativeTracker.view.InitiativeFrame;

import org.leumasjaffe.util.io.PortableScanner;
import edu.wpi.heineman.xml.Message;
import edu.wpi.heineman.client.ServerAccess;

@SuppressWarnings("serial")
public class KillAllAction extends InitiativeTrackerAction implements Action {
	
	public KillAllAction(String text) {
		super(text);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String xmlString = Message.requestHeader();
		
		System.out.print("Are you sure you want to delete all NPC's? ");
		String s = PortableScanner.nextLine();
		if (s.equalsIgnoreCase("y") || s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("true")) {
			Constants.pageNum = 0;
			root.killAll();
			InitiativeFrame.getInstance().reset();
			System.out.println("\nAll NPC entities removed...\nTurn values reset," +
					"id counter preserved.\n");
			xmlString += "<killAllRequest all='false' game='" + Constants.id + "/></request>";
		}
		else if (s.equalsIgnoreCase("delete")) {
			for (CharacterLinker c : list.getList()) {
				c.tearDown();
			}
			list.clear();
			root.gen.setId(0);
			InitiativeFrame.getInstance().reset();
			System.out.println("\nAll entities removed...\nAll fields and" +
					"counters reset to default.\n");
			xmlString += "<killAllRequest all='true' game='" + Constants.id + "'/></request>";
		}
		else return;
		ServerAccess.getInstance().sendRequest(new Message(xmlString));
		
		gui.repaint();
	}
	
}
